package com.shuang.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.shuang.common.JsonUtils;
import com.shuang.common.TreeNode;
import com.shuang.mapper.DbGoodsClassMapper;
import com.shuang.pojo.DbGoodsClass;
import com.shuang.pojo.DbGoodsClassExample;
import com.shuang.pojo.DbGoodsClassExample.Criteria;
import com.shuang.redis.JedisClient;
import com.shuang.service.DbGoodsClassService;

/**
 * 已经加入redis缓存机制
 * @author shuang
 *
 */
@Service("dbGoodsClassService")
public class DbGoodsClassServiceImpl implements DbGoodsClassService {

	@Resource(name = "dbGoodsClassMapper")
	private DbGoodsClassMapper dbGoodsClassMapper;
	@Resource(name = "jedisClient")
	private JedisClient jedisClient;
	@Override
	public List<TreeNode> selClassList(short parentId) {
		//查询缓存
		try {
			String result = jedisClient.hget("class", parentId+"");
			//System.out.println(result);
			if(result!=null&&!result.isEmpty()){
				return JsonUtils.jsonToList(result, TreeNode.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//正常查询
		DbGoodsClassExample example = new DbGoodsClassExample();
		Criteria criteria = example.createCriteria();
		criteria.andCParentidEqualTo(parentId);
		example.setOrderByClause("c_order");
		List<DbGoodsClass> list = dbGoodsClassMapper.selectByExample(example);
		// 分类列表转换成TreeNode的列表
		List<TreeNode> resultList = new ArrayList<TreeNode>();
		for (DbGoodsClass li : list) {
			TreeNode tn = new TreeNode(li.getcId(), li.getcName(),li.getcIsParent() == 1 ? "closed" : "open",li.getcCreateTime());
			resultList.add(tn);
		}
		//保存到缓存
		try {
			jedisClient.hset("class", parentId+"", JsonUtils.objectToJson(resultList));
		} catch (Exception e) {
			e.printStackTrace();
		}

		return resultList;
	}

	@Override
	public Map<String, Object> insertNewClass(short parentId, String cName) throws Exception {
		DbGoodsClassExample example = new DbGoodsClassExample();
		Criteria criteria = example.createCriteria();
		criteria.andCParentidEqualTo(parentId);
		// 查出序号
		int count = dbGoodsClassMapper.countByExample(example);

		DbGoodsClass record = new DbGoodsClass();
		record.setcCreateTime(new Date());
		record.setcIsParent((short) 0);
		record.setcName(cName);
		record.setcParentid(parentId);
		record.setcOrder((short) count++);

		int result = dbGoodsClassMapper.insert(record);
		Map<String, Object> resultData = new HashMap<String, Object>();
		if (result > 0) {
			DbGoodsClass record1 = new DbGoodsClass();
			record1.setcId(Long.valueOf(parentId));
			record1.setcIsParent((short) 1);
			// 如果父节点原来不是父节点需要更新为父节点
			int result1 = dbGoodsClassMapper.updateByPrimaryKeySelective(record1);
			if (result1 > 0) {
				resultData.put("status", 200);
				resultData.put("id", record.getcId());
				try {
					jedisClient.hdel("class",parentId+"");//插入新节点成功，清空缓存
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				resultData.put("status", 500);
				throw new Exception();//失败抛出异常 让切面回滚此次操作
			}
		}
		return resultData;
	}

	@Override
	public Map<String, Object> updateClass(Long cId, String cName,Long parentId) {
		DbGoodsClass record = new DbGoodsClass();
		record.setcId(cId);
		record.setcName(cName);
		record.setcUpdateTime(new Date());
		int result = dbGoodsClassMapper.updateByPrimaryKeySelective(record);
		Map<String, Object> resultData = new HashMap<String, Object>();
		if (result > 0) {
			resultData.put("status", 200);
			try {
				jedisClient.hdel("class",parentId+"");//插入新节点成功，清空缓存
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			resultData.put("status", 500);
		}

		return resultData;
	}

	@Override
	public Map<String, Object> removeClass(Long cId, short parentId) {
		int result = dbGoodsClassMapper.deleteByPrimaryKey(cId);// 删除节点
		Map<String, Object> resultData = new HashMap<String, Object>();
		if (result > 0) {
			DbGoodsClassExample example = new DbGoodsClassExample();
			Criteria criteria = example.createCriteria();
			criteria.andCParentidEqualTo(parentId);
			int count = dbGoodsClassMapper.countByExample(example);// 查询该节点的父节点是否有其他子节点
			if (count <= 0) {// 如果没有
				DbGoodsClass record = new DbGoodsClass();
				record.setcId((long) parentId);
				record.setcIsParent((short) 0);
				record.setcUpdateTime(new Date());
				int result1 = dbGoodsClassMapper.updateByPrimaryKeySelective(record);
				if (result1 > 0) {
					resultData.put("status", 200);
					try {
						jedisClient.hdel("class",parentId+"");//插入新节点成功，清空缓存
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					resultData.put("status", 500);
				}

			} else {
				resultData.put("status", 200);
			}

		} else {
			resultData.put("status", 500);
		}
		return resultData;
	}

	@Override
	public List<TreeNode> selcetAllClass() {
		//查询缓存
		try {
			String result = jedisClient.hget("class", 1+"");
			if(result!=null&&!result.isEmpty()){
				return JsonUtils.jsonToList(result, TreeNode.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		DbGoodsClassExample example = new DbGoodsClassExample();
		Criteria criteria = example.createCriteria();
		criteria.andCParentidEqualTo((short) 1);
		List<DbGoodsClass> result = dbGoodsClassMapper.selectByExample(example);// 查询出所有的节点
		
		List<TreeNode> resultList=new ArrayList<TreeNode>();
		TreeNode node=null;
		for(DbGoodsClass re:result){
			node=new TreeNode(re.getcId(),re.getcName(),re.getcIsParent() == 1 ? "closed" : "open",re.getcCreateTime());
			resultList.add(node);
		}
		//保存到缓存
		try {
			jedisClient.hset("class", 1+"", JsonUtils.objectToJson(resultList));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultList;
	}

}
